//
//  YKAccountTool.m
//  youkan
//
//  Created by mac mini on 16/2/27.
//  Copyright © 2016年 Honsintech. All rights reserved.
//

#import "YKAccountTool.h"
#import "YKAccount.h"
#import "AFNetworking.h"
#import "YKHttpTool.h"
#import "YKAccountParam.h"
#import "MJExtension.h"
#define YKAccoutFileName  [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"accout.data"]
#define YKAuthorizeURLBase      @"https://api.weibo.com/oauth2/authorize"
#define YKClient_id             @"1005853160"
#define YKRedirect_uri          @"https://hao.360.cn/"
#define YKClient_secret         @"da5a4b804363a8b8439176e5eab791a2"
@implementation YKAccountTool
static YKAccount *_account ;
+(void)saveAccount:(YKAccount *)account{
        [NSKeyedArchiver archiveRootObject:account toFile:YKAccoutFileName];
}
+(YKAccount *)account{
    if (!_account) {
        _account =  [NSKeyedUnarchiver unarchiveObjectWithFile:YKAccoutFileName];
//        判断账号是否过期
        if ([[NSDate date] compare: _account.expires_date ]!= NSOrderedAscending) {
            YKLog(@"%@",_account);
            return nil; //账号过期
        }
    }
    return _account;
}
+(void)accountWithCode:(NSString*)code success:(void(^)(void)) success failure:(void(^)(NSError*error))failure{
//    拼接参数
    YKAccountParam *param =[[YKAccountParam alloc]init];
    param.client_id =YKClient_id;
    param.client_secret = YKClient_secret;
    param.grant_type =@"authorization_code";
    param.redirect_uri = YKRedirect_uri;
    param.code = code;

//    //发送post请求 网络的请求和解析  ***param.keyValues 是将对象转成字典***
    [YKHttpTool POST:@"https://api.weibo.com/oauth2/access_token"  parameters:param.keyValues success:^(id responseObject) {
        YKAccount *accout =[YKAccount accountWithDictionary:responseObject];
//        YKLog(@"access_token==%@",accout.access_token);
//        YKLog(@"expires_date==%@",accout.expires_date);
        [YKAccountTool saveAccount:accout];
        if (success) {
            success();
        }
    } failure:^(NSError *error) {
        if (failure) {
            failure(error);
        }
    }];
/*
//    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
//    NSMutableDictionary *params =[NSMutableDictionary dictionary];
//    params[@"client_id"]=YKClient_id;
//    params[@"client_secret"]=YKClient_secret;
//    params[@"grant_type"]=@"authorization_code";
//    params[@"code"]=code;
//    params[@"redirect_uri"]=YKRedirect_uri;
//    [manager POST:@"https://api.weibo.com/oauth2/access_token" parameters:params success:^(AFHTTPRequestOperation *operation, id responseObject) {
//        //        YKLog(@"respons===%@",responseObject);
//        //保存账号信息 字典转模型 业务类
//        YKAccount *accout =[YKAccount accountWithDictionary:responseObject];
//        //        NSString *docPath=[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject];
//        //        NSString *filePath =[[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:@"accout.data"];
//        //        [NSKeyedArchiver archivedDataWithRootObject:YKAccoutFileName];
//
//        [YKAccountTool saveAccount:accout];
//        //        进入首页 ：新特新？tabBar
//        [YKRootTool chooseRootViewController:YKKeyWindow];
//        //
//    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
//        //        YKLog(@"error==%@",error);
//    }];
*/
}

@end
